/********************************** (C) COPYRIGHT *******************************
* File Name          : main.c
* Author             : WCH
* Version            : V1.0.0
* Date               : 2021/06/06
* Description        : Main program body.
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
#include "ch32v30x.h"
#include <rtthread.h>
#include <rthw.h>
#include "drivers/pin.h"
#include <board.h>

/* Global typedef */

/* Global define */


#define LED0_PIN  35   //PC3

/* Global Variable */




/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */


extern void msc_ram_init(void);
extern void cdc_acm_init(void);

extern void app_tm1668_init(void);
extern void app_lcd_menu_init(void);

extern void app_usb_init(void);

extern int board_flash_init(void);

int main(void)
{
    RCC_ClocksTypeDef RCC_Clocks = {0};

    //msc_ram_init();
    //cdc_acm_init();

    rt_kprintf("MCU: CH32V307\n");
	rt_kprintf("SysClk: %dHz\n",SystemCoreClock);

    RCC_GetClocksFreq(&RCC_Clocks);
    rt_kprintf("SysClk: %dHz\n",RCC_Clocks.SYSCLK_Frequency);
    rt_kprintf("HCLK:   %dHz\n",RCC_Clocks.HCLK_Frequency);
    rt_kprintf("PCLK1:  %dHz\n",RCC_Clocks.PCLK1_Frequency);
    rt_kprintf("PCLK2:  %dHz\n",RCC_Clocks.PCLK1_Frequency);
    rt_kprintf("ADCCLK: %dHz\n",RCC_Clocks.ADCCLK_Frequency);
    rt_kprintf("www.wch.cn\n");
	//LED1_BLINK_INIT();

    GPIO_InitTypeDef GPIO_InitStructure = {0};

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    GPIO_ResetBits(GPIOB,GPIO_Pin_4);
	GPIO_ResetBits(GPIOA,GPIO_Pin_15);

	board_flash_init();

    app_usb_init();

	app_lcd_menu_init();
	//app_tm1668_init();

	while(1)
	{
        GPIO_SetBits(GPIOB,GPIO_Pin_4);
	    GPIO_SetBits(GPIOA,GPIO_Pin_15);
	    rt_thread_mdelay(500);
        GPIO_ResetBits(GPIOB,GPIO_Pin_4);
	    GPIO_ResetBits(GPIOA,GPIO_Pin_15);
	    rt_thread_mdelay(500);
	}
}


/*********************************************************************
 * @fn      led
 *
 * @brief   gpio operation by pins driver.
 *
 * @return  none
 */
int led(void)
{
    rt_uint8_t count;

    rt_pin_mode(LED0_PIN, PIN_MODE_OUTPUT);
    rt_kprintf("led_SP:%08x\r\n",__get_SP());
    for(count = 0 ; count < 10 ;count++)
    {
        rt_pin_write(LED0_PIN, PIN_LOW);
        rt_kprintf("led on, count : %d\r\n", count);
        rt_thread_mdelay(500);

        rt_pin_write(LED0_PIN, PIN_HIGH);
        rt_kprintf("led off\r\n");
        rt_thread_mdelay(500);
    }
    return 0;
}

MSH_CMD_EXPORT(led,  led sample by using I/O drivers);






